$(function() {
        //鼠标初始化页面
        $.get("http://127.0.0.1:8400/gouwuche", {
            "inite_shoppingcar": true
        }, function(data) {

            //从服务端获取商品列表并添加到页面
            var cart_group_item = "";
            //从服务端获取并总计商品数据的清单信息
            var common_handler_fixed = "";
            //购物车中总件数
            var total_num = 0;
            //购物车中需要的总价
            var total_price = 0;
            $.each(data, function(index, element) {

                total_num++;
                total_price = Number(element.allPrices) + Number(total_price);
                //循环购物车中的商品,拼接用于进行页面展示
                cart_group_item += `
                <tr class="cart_item ">
                    <td valign="top">
                        <div class="cart_item_desc clearfix">
                            <input type="checkbox" class="js_item_selector cart_item_selector" data-app="all" checked="'checked'/">
                            <div class="cart_item_desc_wrapper">
                                <a class="cart_item_pic" href="http://item.jumeiglobal.com/ht20191121145415p4970446.html?from=jmglobal" target="_blank"> 
                                    <img src="` + element.img + `"> 
                                    <span class="sold_out_pic png"></span> 
                                </a> <a class="cart_item_link" href="http://item.jumeiglobal.com/ht20191121145415p4970446.html?from=jmglobal" target="_blank">` + element.name + `</a>
                                <div class="sale_info clearfix"> </div>
                            </div>
                        </div>
                    </td>
                    <td>
                        <div class="cart_item_price">
                            <p class="jumei_price">` + element.money + `</p>
                        </div>
                    </td>
                    <td>
                        <div class="cart_item_num ">
                            <div class="item_quantity_editer clearfix" data-item-key="7011255730_ht20200323154517p4982140">
                             <span class="increase_one" onclick="jian(this)">-</span>
                             <input class="item_quantity" type="text" value=${element.num}> 
                             <span class="increase_one" onclick="jia(this)">+</span> </div>
                            <div class="item_shortage_tip"></div>
                        </div>
                    </td>
                    <td>
                        <div class="cart_item_total">
                            <p class="item_total_price">` + element.allPrices + `</p>
                        </div>
                    </td>
                    <td>
                        <div class="cart_item_option">
                            <a class="icon_small delete_item png" data-item-key="7011266336_ht20191029164211p5001106" href="javascript:deleteShopping()" title="删除">删除</a>
                        </div>
                    </td>
                    
                </tr>
            `;
            })
            $(".cart_group_item>tbody").html(cart_group_item);
            $(".common_handler").html(`
            <div class="right_handler"> 共 <span class="total_amount"> ` + total_num + ` </span> &nbsp;件商品 &nbsp;&nbsp; 商品应付总额：¥<span class="total_price">` + total_price + `</span> <a id="go_to_order" class="btn" href="javascript:add();">去结算</a> </div> <label for="js_all_selector" class="cart_all_selector_wrapper"> <input type="checkbox" id="js_all_selector" class="js_all_selector all_selector" checked="checked> 全选 </label>                    <a class="go_back_shopping" href="http://www.jumeiglobal.com/">继续购物</a> <span class="seperate_line">|</span> <a class="clear_cart_all" href="">清空选中商品</a>
        `)
        }, "JSON");


    })
    /* 删除购物车 */
function deleteShopping() {
    var $del_cart_item = $(".cart_item_option");
    $del_cart_item.on("click", function() {
        $(this).parent().parent().remove();
        let money = $(this).parent().siblings("td").find(".item_total_price").html();

        let name = $(this).parent().siblings("td").find(".cart_item_link").html();
        //总数目
        let num = $(".total_amount").html();
        $(".total_amount").html(Number(num -= 1));
        //总价
        let allMoney = $(".total_price").html();
        allMoney = Number(allMoney) - Number(money);

        $(".total_price").html(allMoney);
        axios({
            method: "get",
            url: "http://127.0.0.1:8400/deleteShopping",
            params: {
                name: name
            }
        }).then(response => {
            let bool = response.data;
            console.log(bool);
        })

    })
}
/* 加 */
function jia(e) {
    //商品名称
    let name = $(e).parent().parent().parent().siblings("td").find(".cart_item_link").html();
    $(e).siblings("input").val(Number($(e).siblings("input").val()) + 1);
    //原价
    let money = $(e).parent().parent().parent().siblings("td").find(".jumei_price").html();
    //原价*数量
    let allMoney = Number(money) * $(e).siblings("input").val();

    $(e).parent().parent().parent().siblings("td").find(".item_total_price").html(allMoney);
    $(".total_price").html(Number($(".total_price").html()) + Number(money));
    axios({
        method: "get",
        url: "http://127.0.0.1:8400/increase",
        params: {
            bool: true, //增加
            name: name, //商品名称
            num: $(e).siblings("input").val(), //数量
            allPrices: allMoney, //总价
        }
    }).then(response => {
        console.log(response.data);
    })
}
/* 减 */
function jian(e) {
    //商品名称
    let name = $(e).parent().parent().parent().siblings("td").find(".cart_item_link").html();
    if ((Number($(e).siblings("input").val()) - 1) != 0) {
        $(e).siblings("input").val(Number($(e).siblings("input").val()) - 1);
        //原价
        let money = $(e).parent().parent().parent().siblings("td").find(".jumei_price").html();
        //原价*数量
        let allMoney = Number(money) * $(e).siblings("input").val();

        $(e).parent().parent().parent().siblings("td").find(".item_total_price").html(allMoney);
        $(".total_price").html(Number($(".total_price").html()) - Number(money));
        axios({
            method: "get",
            url: "http://127.0.0.1:8400/increase",
            params: {
                bool: true, //增加
                name: name, //商品名称
                num: $(e).siblings("input").val(), //数量
                allPrices: allMoney, //总价
            }
        }).then(response => {
            console.log(response.data);
        })
    } else {
        return false;
    }

}

function add() {
    axios({
        method: "get",
        url: "http://127.0.0.1:8400/addOrder",
    }).then(response => {
        window.location = "../comfirmorder.html";
    })
}